package com.travel.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.travel.commons.shiro.ShiroUserGetter;
import com.travel.commons.utils.BeanUtils;
import com.travel.commons.utils.SnowNoGenerator;
import com.travel.commons.utils.PageUtils;
import com.travel.commons.utils.R;
import com.travel.commons.validator.ValidatorUtils;
import com.travel.commons.validator.groups.AddGroup;
import com.travel.modules.asset.entity.Strategy;
import com.travel.modules.asset.service.StrategyService;
import com.travel.modules.sys.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/strategy")
public class SysStrategyController {
    @Resource
    private StrategyService strategyService;

    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/list")
    private R list(@RequestBody Map<String, String> param) {
        List<Strategy> list = strategyService.getAllStrategy(param);
        return R.ok().put("page", list);
    }

    @RequestMapping("/list/current")
    private R list2Current() {
        List<Strategy> list = strategyService.list(new QueryWrapper<Strategy>().eq("user_id", ShiroUserGetter.getUserId()));
        return R.ok().put("page", list);
    }

    @RequestMapping("/info")
    private R info(@RequestBody Strategy form) {
        String strategyId = form.getStrategyId();
        if (StringUtils.isBlank(strategyId)) {
            return R.error("缺少id");
        }
        Strategy strategy = strategyService.getOne(new QueryWrapper<Strategy>().eq("strategy_id", strategyId).orderByDesc("create_time"));
        strategy.setUsername(sysUserService.getById(strategy.getUserId()).getUsername());
        return R.ok().put("page", strategy);
    }

    /**
     * 新增攻略
     * @param form title - 标题
     *             content - 内容
     *             urls - 图片url数组
     */
    @RequestMapping("/add")
    public R add(@RequestBody Strategy form) {
        ValidatorUtils.validate(form, AddGroup.class);
        form.setStrategyId("s-" + new SnowNoGenerator().generate());
        form.setUserId(form.getUserId() != null ? form.getUserId() : ShiroUserGetter.getUserId());
        String[] urls = form.getUrls();
        String pics = StringUtils.join(urls, "&*");
        if (pics != null) {
            form.setPics(pics);
        }
        strategyService.save(form);
        return R.ok();
    }

    @RequestMapping("/edit")
    private R edit(@RequestBody Strategy form) {
        String strategyId = form.getStrategyId();
        if (StringUtils.isBlank(strategyId)) {
            return R.error("攻略id不能为空");
        }

        Strategy old = strategyService.getOne(new QueryWrapper<Strategy>().eq("strategy_id", strategyId));
        if (old == null) {
            return R.error("请检查文章是否存在");
        }
        form.setStrategyId(null);
        form.setUserId(null);
        String[] urls = form.getUrls();
        String pics = StringUtils.join(urls, "&*");
        if (pics != null) {
            form.setPics(pics);
        }
        BeanUtils.copyProperties(form, old);
        strategyService.updateById(old);
        return R.ok();
    }

    @RequestMapping("del")
    private R del(@RequestBody Strategy form) {
        String strategyId= form.getStrategyId();
        if (StringUtils.isBlank(strategyId)) {
            return R.error("攻略id不能为空");
        }

        strategyService.remove(new QueryWrapper<Strategy>().eq("strategy_id", strategyId));
        return R.ok();
    }
}
